package com.oa.works.aop;

import com.oa.works.common.util.R;
import com.oa.works.config.shiro.ThreadLocalToken;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 验证返回的线程里令牌是否刷新
 * AOP切面类
 * @date 2021/10/14 16:31
 * @author Qin
 */
@Aspect
@Component
public class TokenAspect {

    @Autowired
    private ThreadLocalToken threadLocalToken;

    /**
     * 设置web拦截规则
     * Pointcut：是植入Advice的触发条件
     */
    @Pointcut("execution(public * com.oa.works.controller.*.*(..))")
    public void aspect(){

    }

    /**
     * 验证token
     * Around：环绕增强，相当于MethodInterceptor
     * 环绕通知=前置+目标方法执行+后置通知
     */
    @Around("aspect()")
    public Object around(ProceedingJoinPoint point) throws Throwable{
        R r =(R)point.proceed();
        String token = threadLocalToken.getToken();
        if (token!=null){
            //proceed方法就是用于启动目标方法执行的.
            r.put("token",token);
            threadLocalToken.clear();
        }
        return r;
    }


}
